package com.zhl.media.app.mvp.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.bumptech.glide.Glide;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;

import com.orhanobut.hawk.Hawk;
import com.zhl.media.app.di.component.DaggerUserInfoComponent;
import com.zhl.media.app.di.module.UserInfoModule;
import com.zhl.media.app.greendao.AdJustDao;
import com.zhl.media.app.greendao.ElevatorDao;
import com.zhl.media.app.greendao.MediaPhotoDao;
import com.zhl.media.app.greendao.MediaPositionDao;
import com.zhl.media.app.greendao.ProjDao;
import com.zhl.media.app.model.MediaPhoto;
import com.zhl.media.app.mvp.contract.UserInfoContract;
import com.zhl.media.app.mvp.presenter.UserInfoPresenter;

import com.zhl.media.app.R;
import com.zhl.media.app.utils.EntityManager;
import com.zhl.media.app.utils.TagAliasOperatorHelper;
import com.zhl.media.commonres.base.AppActivity;
import com.zhl.media.commonres.dialog.ConfirmDialog;
import com.zhl.media.commonres.view.BaseItemView;
import com.zhl.media.commonres.view.PublicTitleBar;
import com.zhl.media.commonsdk.core.RouterHub;
import com.zhl.media.commonsdk.data.UserUtil;
import com.zhl.media.commonsdk.utils.DeleteFileUtil;
import com.zhl.media.commonsdk.utils.Utils;


import java.util.List;
import java.util.Map;

import butterknife.BindView;
import cn.jpush.android.api.JPushInterface;

import static com.jess.arms.utils.Preconditions.checkNotNull;
import static com.zhl.media.app.utils.TagAliasOperatorHelper.ACTION_CLEAN;
import static com.zhl.media.app.utils.TagAliasOperatorHelper.ACTION_DELETE;
import static com.zhl.media.app.utils.TagAliasOperatorHelper.ACTION_SET;


@Route(path = RouterHub.APP_USERINFOACTIVITY)
public class UserInfoActivity extends AppActivity<UserInfoPresenter> implements UserInfoContract.View {
    @BindView(R.id.titleBar)
    public PublicTitleBar titleBar;
    @BindView(R.id.name)
    BaseItemView name;
    @BindView(R.id.login_name)
    BaseItemView login_name;
    @BindView(R.id.org)
    BaseItemView org;
    @BindView(R.id.logOut)
    Button logOut;


    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        DaggerUserInfoComponent.builder().appComponent(appComponent).userInfoModule(new UserInfoModule(this)).build().inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_user_info; //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        titleBar.setTitle("个人中心");
        mPresenter.getUserInfo();
        logOut.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MediaPhotoDao mediaPhotoDao = EntityManager.getInstance(UserInfoActivity.this).getMediaPhotoDao();
                ProjDao projDao = EntityManager.getInstance(UserInfoActivity.this).getProjDao();
                ElevatorDao elevatorDao = EntityManager.getInstance(UserInfoActivity.this).getElevatorDao();
                MediaPositionDao mediaPositionDao = EntityManager.getInstance(UserInfoActivity.this).getMediaPositionDao();
                AdJustDao adJustDao = EntityManager.getInstance(UserInfoActivity.this).getAdJustDao();
                ConfirmDialog confirmDialog = new ConfirmDialog(UserInfoActivity.this,"消息提示","退出后将删除离线数据及未上刊结果，是否继续退出？","取消","确定");
                confirmDialog.show();
                confirmDialog.setListener(new ConfirmDialog.HallListener() {
                    @Override
                    public void confirm() {
                        List<MediaPhoto> list = mediaPhotoDao.loadAll();
                        for (int i = 0; i < list.size(); i++) {
                            DeleteFileUtil.deleteFile(list.get(i).getPhotoPath());
                        }
                        mediaPhotoDao.deleteAll();
                        projDao.deleteAll();
                        elevatorDao.deleteAll();
                        mediaPositionDao.deleteAll();
                        adJustDao.deleteAll();
                        TagAliasOperatorHelper.TagAliasBean tagAliasBean = new TagAliasOperatorHelper.TagAliasBean();
                        tagAliasBean.action = ACTION_DELETE;
                        tagAliasBean.alias = UserUtil.getUser().getId();
                        tagAliasBean.isAliasAction = true;
                        TagAliasOperatorHelper.getInstance().handleAction(getApplicationContext(), 1, tagAliasBean);
                        Hawk.deleteAll();
                        ArmsUtils.killAll();
                        Utils.navigation(UserInfoActivity.this,RouterHub.LOGIN_LOGINACTIVITY);
                    }
                });
            }
        });
    }

    @Override
    public void showLoading() {
        showDiglog();
    }

    @Override
    public void hideLoading() {
        hideDiglog();
    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.snackbarText(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }

    @Override
    public void onSuccess(Map map) {
        name.setRightText((String)map.get("fullName"));
        org.setRightText((String)map.get("deptName"));
        login_name.setRightText((String)map.get("userName"));
    }
}
